package com.coldchain.admin.config.api;


import com.coldchain.admin.config.entity.GoodsClassify;
import com.coldchain.admin.config.service.IGoodsClassifyService;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.ObjectRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-11-09
 */
@RestController
@RequestMapping("/config/goods/Classify")
public class GoodsClassifyController {
    private static final Logger log= LoggerFactory.getLogger(GoodsClassifyController.class);
    @Autowired
    public IGoodsClassifyService goodsClassifyService;


    @GetMapping("/all")
    public Object getClassifysTree(){
        try{
            return new ObjectRestResponse().data(goodsClassifyService.getGoodsClassTree()).succeed();
        }catch (Exception e){
            log.error("查询商器类别异常,error:{}",e.getMessage());
            return new ObjectRestResponse<>().error("查询失败");
        }

    }

    @PostMapping("/edit")
    @CheckUserToken
    public Object edit(GoodsClassify goodsClassify){
        try{
            goodsClassifyService.edit(goodsClassify);
            return new ObjectRestResponse<>().succeed("ok");
        }catch (Exception e){
            log.error("编辑商器类别异常,error:{}",e.getMessage());
            return new ObjectRestResponse<>().error("编辑失败");
        }

    }


    @PostMapping("/del/{goodsClassifyId}")
    @CheckUserToken
    public Object del(@PathVariable("goodsClassifyId") Integer goodsClassifyId){
        try{
            boolean isOk=goodsClassifyService.removeById(goodsClassifyId);
            return new ObjectRestResponse<>().succeed("ok");
        }catch (Exception e){
            log.error("删除商器类别异常,error:{}",e.getMessage());
            return new ObjectRestResponse<>().error("编辑失败");
        }

    }
}

